package com.qfedu.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qfedu.common.entity.User;
import com.qfedu.common.utils.JwtUtils;
import com.qfedu.common.utils.TokenUtils;
import com.qfedu.common.utils.UserUtils;
import com.qfedu.user.dao.UserMapper;
import com.qfedu.user.param.LoginParam;
import com.qfedu.user.service.UserService;
import com.qfedu.user.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * --- 业精于勤,荒于嬉 ---
 *
 * @author Song
 * @date 2024/9/9
 * @desc First
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    // 注入其他模块的业务层
    // @Resource
    // private PointService pointService;
    //
    // @Resource
    // private PointRuleService pointRuleService;
    // @Resource
    // private ThreadPoolExecutor threadPoolExecutor;
    //
    // @Resource
    // private RedisUtil redisUtil;

    @Override
    public String login(LoginParam loginParam) {
        // 判断用户名和密码
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, loginParam.getUsername()));
        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }
        if (!user.getPassword().equals(loginParam.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        // 验证成功，返回token(jwt)
        Map<String, Object> claims = new HashMap<>();
        claims.put("uid", user.getId());
        String jwt = JwtUtils.createJwt(claims);

        return jwt;
    }

    @Override
    public UserVO my(Integer uid) {
        // 根据id查询用户信息
        User user = userMapper.selectById(uid);

        UserVO userVO = new UserVO();
        userVO.setGender("男");
        // 队vo中的一些用户基本数据进行拷贝赋值
        BeanUtils.copyProperties(user, userVO);


        // 获取签到状态，暂时写死
        userVO.setSignFlag(0);
        return userVO;
    }


    ///////////////////////////////////////////////////////////////////////

    @Override
    public void userInfo(String token) {
        // Claims claims = JwtUtils.parseJWT(token);
        // Integer uid = (Integer) claims.get("uid");
        // UserUtils.setId(uid);

        // 通过ThreadLocal取用户id
        // Integer uid = UserUtils.getId();
        // userMapper.selectById(uid);
    }


}
